﻿package com.wandian.view.components.ui
{
	import com.wandian.ApplicationFacade;
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.AccountProxy;
	import com.wandian.model.vo.FaceVo;
	import com.wandian.model.vo.PropVo;
	import com.wandian.model.vo.RoomVo;
	import com.wandian.util.CLZLlistScrollPanel;
	import com.wandian.util.CLZLoadIcon;
	import com.wandian.util.CLZTabBar;
	import com.wandian.util.CLZZoom;
	import com.wandian.util.GlowFilterManage;
	import com.wandian.util.ToolTip;
	import com.wandian.util.UIHelper;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	import org.puremvc.as3.patterns.facade.Facade;

	/**
	* @author long
	* @QQ 108232706
	* @version 1.0
	*/
	public class FacePanel extends Object
	{
		private var facade:ApplicationFacade;
		private var _global:GameData;
		private var _filter:GlowFilterManage;
		private var _mcPanel:Sprite;
		private var _tabBar:CLZTabBar;
		private var _contentMc:Sprite;

		public function FacePanel(mcPanel:Sprite)
		{
			this.facade=ApplicationFacade.getInstance();
			this._global=GameData.getInstance();
			this._filter=GlowFilterManage.getInstance();
			this._contentMc=new Sprite();
			this._mcPanel=mcPanel;
			ToolTip.stage=Texas.I._mcTipView.stage;
			this._mcPanel.addChild(this._contentMc);
			this._contentMc.x=20;
			this._contentMc.y=57;
			var tabs:Array=[[this._mcPanel["btn1"], GameData.tip["FACE_COMMON_NAME"]], [this._mcPanel["btn2"], GameData.tip["FACE_COMMON_NAME_1"]]];
			this._tabBar=new CLZTabBar(tabs);
			var index:int=0;
			while (index < tabs.length)
			{

				tabs[index][0].addEventListener(Event.CHANGE, this.changeTabBarHandler);
				index=index + 1;
			}
			this._tabBar.setClickIndex(this._mcPanel["btn1"]);
			return;
		}

		private function changeTabBarHandler(event:Event):void
		{
			var arrFaceVo:Array=null;
			var canUse:Boolean=false;
			var account:AccountProxy=null;
			var proVo:PropVo=null;
			switch (event.target.name)
			{
				case "btn1":
				{
					canUse=false;
					account=this.facade.retrieveProxy(AccountProxy.NAME) as AccountProxy;
					if (account.getPropNumById(2, 1) == 0)
					{
						canUse=false;
						//暂不可使用，{font color='#ff0000'}{u}点击购买
						this._mcPanel["tip_txt"].htmlText="<a href=\"event:BUY_IN\">" + GameData.replaceStrTipHtml(GameData.tip["FACE_TIP_BUYIN"]) + "</a>";
						this._mcPanel["tip_txt"].addEventListener(TextEvent.LINK, this.linkTxtHandler);
					}
					else
					{
						canUse=true;
						//剩余有效时间：{font color='#ffff00'}%NUM%{/font}天
						account=this.facade.retrieveProxy(AccountProxy.NAME) as AccountProxy;
						proVo=account.getPropById(2, 1);
						this._mcPanel["tip_txt"].htmlText=GameData.replaceStrValue(GameData.tip["FACE_TIP_TIME"], "NUM", proVo.validity);
					}
					arrFaceVo=GameData.getFaceByPcate(0);
					this.showFaceList(arrFaceVo, canUse);
					break;
				}
				case "btn2":
				{
					if (this._global.userInfo.userVo.vip)
					{
						canUse=true;
						//距VIP到期时间还剩{font color='#ffff00'}%NUM%{/font}天
						this._mcPanel["tip_txt"].htmlText=GameData.replaceStrValue(GameData.tip["VIP_DAY_TIP"], "NUM", GameData.countTime(this._global.userInfo.userVo.viptime, 3));
					}
					else
					{
						canUse=false;
						//尊贵的VIP会员才能使用<
						this._mcPanel["tip_txt"].htmlText=GameData.tip["FACE_COMMON_NAME_2_TIP"];
					}
					arrFaceVo=GameData.getFaceByPcate(1);
					this.showFaceList(arrFaceVo, canUse);
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function linkTxtHandler(event:TextEvent):void
		{
			var _loc_2:PropVo=null;
			if (event.text == "BUY_IN")
			{
				_loc_2=GameData.getPropById(2, 1);
				this.facade.sendNotification(CLZNoticeName.BUYIN_PROP_TIP_WINDOW, _loc_2);
			}
			return;
		}

		private function showFaceList(arrFaceVo:Array, canUse:Boolean=false):void
		{
			var faceVo:FaceVo=null;
			var mcFace:MovieClip=null;
			GameData.removeNumChildre(this._contentMc);
			var listScroll:CLZLlistScrollPanel=new CLZLlistScrollPanel(5, 50, 50, 7, 7, 227, 291);
			var total:int=arrFaceVo.length;
			var index:int=0;
			while (index < total)
			{

				faceVo=arrFaceVo[index];
				mcFace=new MovieClip();
				CLZLoadIcon.addIcon(mcFace, faceVo.icon, 50, 50, 0, 0);
//				this._contentMc.addChild(mcFace);
//				break;
				listScroll.setMc(mcFace, index, total);
				mcFace.mouseChildren=false;
				mcFace.buttonMode=true;
				mcFace.faceVo=faceVo;
				ToolTip.setDOInfo(mcFace, faceVo.name);
				mcFace.addEventListener(MouseEvent.CLICK, this.onFaceHandler);
				mcFace.addEventListener(MouseEvent.MOUSE_OUT, this.outFaceHandler);
				mcFace.addEventListener(MouseEvent.MOUSE_OVER, this.overFaceHandler);
				if (canUse)
				{
					mcFace.filters=[];
				}
				else
				{
					mcFace.filters=this._filter.getGrayscale();
				}
				index=index + 1;
			}
			this._contentMc.addChild(listScroll);
			return;
		}

		private function outFaceHandler(event:MouseEvent):void
		{
			ToolTip.hideToolTip();
			GameData.removeNumChildre(this._mcPanel["preview_mc"]);
			this._mcPanel["preview_mc"].visible=false;
			return;
		}

		private function overFaceHandler(event:MouseEvent):void
		{
			ToolTip.showToolTip();
			var _loc_2:*=event.target as MovieClip;
			GameData.removeNumChildre(this._mcPanel["preview_mc"]);
			var _loc_3:*=GameData.getMovieClip(_loc_2.faceVo.ClassSWF) as MovieClip;
			CLZZoom.imageZoomWH(_loc_3, 85, 85);
			this._mcPanel["preview_mc"].addChild(_loc_3);
			this._mcPanel["preview_mc"].visible=true;
			return;
		}

		private function onFaceHandler(event:MouseEvent):void
		{
			var accout:AccountProxy=null;
			var propVo:PropVo=null;
			var mcFace:MovieClip=event.target as MovieClip;
			var faceVo:FaceVo=mcFace.faceVo;
			trace(faceVo.pcate, faceVo.pframe);
			this.facade.sendNotification(CLZNoticeName.HIDE_FACE_PANEL);
			if (faceVo.pcate == 1)
			{
				if (this._global.userInfo.userVo.vip)
				{
					this.facade.sendNotification(CLZNoticeName.FACE_REQ, faceVo);
				}
				else
				{
					//尊贵的VIP会员才能使用
					this.facade.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.tip["FACE_COMMON_NAME_2_TIP"]});
				}
			}
			else
			{
				accout=this.facade.retrieveProxy(AccountProxy.NAME) as AccountProxy;
				this.facade.sendNotification(CLZNoticeName.FACE_REQ, faceVo);
				//需要购买才能使用
//				if (accout.getPropNumById(2, 1) == 0)
//				{
//					propVo=GameData.getPropById(2, 1);
//					this.facade.sendNotification(CLZNoticeName.BUYIN_PROP_TIP_WINDOW, propVo);
//				}
//				else
//				{
//					this.facade.sendNotification(CLZNoticeName.FACE_REQ, faceVo);
//				}
			}
			return;
		}

	}
}
